#!/bin/bash

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    DIRTY="-dirty"
fi

COMMIT=$(git rev-parse --short HEAD)
GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)}

if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
    VERSION=$GIT_TAG
else
    VERSION="${COMMIT}${DIRTY}"
fi

CHART_VERSION=$(echo ${VERSION}|sed 's/\(^[A-Za-z0-9].*\)-.*/\1/')
CHART_REPO=$(echo ${VERSION}|sed 's/^[0-9A-Za-z].*-\([a-z0-9A-Z].*\)/\1/')

## Everything goes to latest..
if [[ "${CHART_REPO}" == "dirty" || "${CHART_REPO}" == "${CHART_VERSION}" ]]; then
    CHART_REPO=latest
fi

if [ -z "$ARCH" ]; then
    ARCH=amd64
fi
